package util;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

public class UpdateParameter {
	
	public void setLastSpiderURLBeginner(String lastSpiderBeginner) {
		String xmlpath = "spiderParameter.xml";
		SAXBuilder builder = new SAXBuilder(false);
		try {
			Document doc = builder.build(xmlpath);
			Element wycParameter = doc.getRootElement();
			List wycParameterlist = wycParameter.getChildren("sourceSite");
			for (Iterator iter = wycParameterlist.iterator();iter.hasNext();) {
				Element sourceSite = (Element) iter.next();
				//sourceSite.getChild("spiderURLTagBeginner").setText(lastSpiderBeginner);		
				List contentSpiderList = sourceSite.getChildren("urlSpider");
				for(Iterator iter2 = contentSpiderList.iterator();iter2.hasNext();){
					Element contentSpider = (Element) iter2.next();
					contentSpider.getChild("spiderURLTagBeginner").setText(lastSpiderBeginner);
				}
			}
			XMLOutputter outputter=new XMLOutputter();
			 outputter.output(doc,new FileOutputStream(xmlpath));

		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void setLastSpiderContentBeginner(String lastSpiderBeginner) {
		String xmlpath = "spiderParameter.xml";
		SAXBuilder builder = new SAXBuilder(false);
		try {
			Document doc = builder.build(xmlpath);
			Element wycParameter = doc.getRootElement();
			List wycParameterlist = wycParameter.getChildren("sourceSite");
			for (Iterator iter = wycParameterlist.iterator();iter.hasNext();) {
				Element sourceSite = (Element) iter.next();
				//sourceSite.getChild("spiderURLTagBeginner").setText(lastSpiderBeginner);		
				List contentSpiderList = sourceSite.getChildren("contentSpider");
				for(Iterator iter2 = contentSpiderList.iterator();iter2.hasNext();){
					Element contentSpider = (Element) iter2.next();
					contentSpider.getChild("contentSpiderBeginner").setText(lastSpiderBeginner);
				}
			}
			XMLOutputter outputter=new XMLOutputter();
			 outputter.output(doc,new FileOutputStream(xmlpath));

		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void setWYCId(String lastSpiderBeginner) {
		String xmlpath = "spiderParameter.xml";
		SAXBuilder builder = new SAXBuilder(false);
		try {
			Document doc = builder.build(xmlpath);
			Element wycParameter = doc.getRootElement();
			List wycParameterlist = wycParameter.getChildren("sourceSite");
			for (Iterator iter = wycParameterlist.iterator();iter.hasNext();) {
				Element sourceSite = (Element) iter.next();
				//sourceSite.getChild("spiderURLTagBeginner").setText(lastSpiderBeginner);		
				List WYCIdList = sourceSite.getChildren("wyc");
				for(Iterator iter2 = WYCIdList.iterator();iter2.hasNext();){
					Element contentSpider = (Element) iter2.next();
					contentSpider.getChild("wycBeginner").setText(lastSpiderBeginner);
				}
			}
			XMLOutputter outputter=new XMLOutputter();
			 outputter.output(doc,new FileOutputStream(xmlpath));

		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void setPUId(String lastSpiderBeginner) {
		String xmlpath = "spiderParameter.xml";
		SAXBuilder builder = new SAXBuilder(false);
		try {
			Document doc = builder.build(xmlpath);
			Element wycParameter = doc.getRootElement();
			List wycParameterlist = wycParameter.getChildren("sourceSite");
			for (Iterator iter = wycParameterlist.iterator();iter.hasNext();) {
				Element sourceSite = (Element) iter.next();
				//sourceSite.getChild("spiderURLTagBeginner").setText(lastSpiderBeginner);		
				List WYCIdList = sourceSite.getChildren("produceURL");
				for(Iterator iter2 = WYCIdList.iterator();iter2.hasNext();){
					Element contentSpider = (Element) iter2.next();
					contentSpider.getChild("puCount").setText(lastSpiderBeginner);
				}
			}
			XMLOutputter outputter=new XMLOutputter();
			 outputter.output(doc,new FileOutputStream(xmlpath));

		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
